#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT(picasso, 2.3, onecoolx@gmail.com)

VERSION_MAJOR=2
VERSION_MINOR=3
VERSION_MICRO=2
VERSION=$VERSION_MAJOR.$VERSION_MINOR.$VERSION_MICRO
AC_SUBST(VERSION_MAJOR)
AC_SUBST(VERSION_MINOR)
AC_SUBST(VERSION_MICRO)
AC_SUBST(VERSION)

AM_INIT_AUTOMAKE(AC_PACKAGE_NAME, $VERSION)
AC_CONFIG_MACRO_DIR(m4)
AC_CONFIG_HEADER(pconfig.h)

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_PROG_LIBTOOL

AC_PREFIX_DEFAULT(/usr/local)

# User define macro.
build_freetype2="yes"
build_fontconfig="yes"
build_fastcopy="yes"
build_sysmalloc="no"
build_lowmem="no"
build_coverage="no"

format_rgba="yes"
format_argb="yes"
format_abgr="yes"
format_bgra="yes"
format_rgb="yes"
format_bgr="yes"
format_rgb565="yes"
format_rgb555="yes"

build_image="yes"
build_gif_support="yes"
build_jpg_support="yes"
build_png_support="yes"
build_webp_support="yes"

build_gles2_backend="yes"

# Check user define macro.
AC_ARG_ENABLE(freetype2,
[  --enable-freetype2     Build FreeType2 support  <default=yes>],
build_freetype2=$enableval)

AC_ARG_ENABLE(fontconfig,
[  --enable-fontconfig     Build FontConfig support  <default=yes>],
build_fontconfig=$enableval)

AC_ARG_ENABLE(lowmem,
[  --enable-lowmem     Build Low Memory used support  <default=no>],
build_lowmem=$enableval)

AC_ARG_ENABLE(fastcopy,
[  --enable-fastcopy     Build Fast Memory Copy used support  <default=yes>],
build_fastcopy=$enableval)

AC_ARG_ENABLE(coverage,
[  --enable-coverage     Build Code coverage test  <default=no>],
build_coverage=$enableval)

AC_ARG_ENABLE(sysmalloc,
[  --enable-sysmalloc     Build System Memory Allocator
                         (new/delete/malloc/free/realloc/calloc) used support  <default=no>],
build_sysmalloc=$enableval)

AC_ARG_ENABLE(rgba,
[  --enable-rgba     Pixel format RGBA support  <default=yes>],
format_rgba=$enableval)

AC_ARG_ENABLE(argb,
[  --enable-argb     Pixel format ARGB support  <default=yes>],
format_argb=$enableval)

AC_ARG_ENABLE(abgr,
[  --enable-abgr     Pixel format ABGR support  <default=yes>],
format_abgr=$enableval)

AC_ARG_ENABLE(bgra,
[  --enable-bgra     Pixel format BGRA support  <default=yes>],
format_bgra=$enableval)

AC_ARG_ENABLE(rgb,
[  --enable-rgb     Pixel format RGB support  <default=yes>],
format_rgb=$enableval)

AC_ARG_ENABLE(bgr,
[  --enable-bgr     Pixel format BGR support  <default=yes>],
format_bgr=$enableval)

AC_ARG_ENABLE(rgb565,
[  --enable-rgb565     Pixel format RGB565 support  <default=yes>],
format_rgb565=$enableval)

AC_ARG_ENABLE(rgb555,
[  --enable-rgb555     Pixel format RGB555 support  <default=yes>],
format_rgb555=$enableval)

AC_ARG_ENABLE(image,
[  --enable-image     Image encoder and decoder support  <default=yes>],
build_image=$enableval)

AC_ARG_ENABLE(gif,
[  --enable-gif     Gif Image support  <default=yes>],
build_gif_support=$enableval)

AC_ARG_ENABLE(png,
[  --enable-png     Png Image support  <default=yes>],
build_png_support=$enableval)

AC_ARG_ENABLE(jpg,
[  --enable-jpg     Jpeg Image support  <default=yes>],
build_jpg_support=$enableval)

AC_ARG_ENABLE(webp,
[  --enable-webp     WebP Image support  <default=yes>],
build_webp_support=$enableval)

AC_ARG_ENABLE(gles2,
[  --enable-gles2   OpenGL ES 2.0 Backend support <default=yes>],
build_gles2_backend=$enableval)

# Checks for libraries.

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(memory.h stddef.h stdlib.h string.h)

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_C_CONST
AC_C_INLINE
AC_TYPE_SIZE_T

# Checks for library functions.
AC_FUNC_MALLOC
AC_CHECK_FUNCS(floor memmove memset pow sqrt strncpy strncasecmp)

# Checks for User define macro.
have_freetype2="no"
if test "x$build_freetype2" = "xyes"; then
    AC_CHECK_LIB(freetype, FT_New_Face, [
        CFLAGS="$CFLAGS `pkg-config freetype2 --cflags`"
        CPPFLAGS="$CPPFLAGS `pkg-config freetype2 --cflags`"
        CXXFLAGS="$CXXFLAGS `pkg-config freetype2 --cflags`"
        LIBS="$LIBS `pkg-config freetype2 --libs`"
        AC_CHECK_HEADERS(ft2build.h, [
            have_freetype2="yes"
            AC_DEFINE(ENABLE_FREE_TYPE2, 1, [Define if FreeType2 is supported.])
       ])
    ])
    if test "x$have_freetype2" = "xno"; then
        AC_MSG_ERROR([freetype2 headers and/or libs where not found, install them or build with --disable-freetype2 on Windows])
    fi
else
	LIBS="$LIBS -lgdi32"
fi

if test "x$build_fontconfig" = "xyes"; then
    AC_CHECK_LIB(fontconfig, FcInit, [
        CFLAGS="$CFLAGS `pkg-config fontconfig --cflags`"
        CPPFLAGS="$CPPFLAGS `pkg-config fontconfig --cflags`"
        CXXFLAGS="$CXXFLAGS `pkg-config fontconfig --cflags`"
        LIBS="$LIBS `pkg-config fontconfig --libs`"
        AC_CHECK_HEADERS(fontconfig/fontconfig.h, [
            AC_DEFINE(ENABLE_FONT_CONFIG, 1, [Define if FontConfig is supported.])
        ])
    ])
fi

if test "x$build_gles2_backend" = "xyes"; then
    AC_CHECK_LIB(GLESv2, glGetString, [
        LIBS="$LIBS -lGLESv2"
        AC_CHECK_HEADERS(GLES2/gl2.h, [
            AC_DEFINE(ENABLE_GLES2_SUPPORT, 1, [Define if gles 2.0 backend is supported.])
        ])
    ])
fi

if test "x$build_lowmem" = "xyes"; then
    AC_DEFINE(ENABLE_LOW_MEMORY, 1, [Define if Low memory is supported.])
fi

if test "x$build_fastcopy" = "xyes"; then
    AC_DEFINE(ENABLE_FAST_COPY, 1, [Define if Fast memory copy is supported.])
fi

if test "x$build_sysmalloc" = "xyes"; then
    AC_DEFINE(ENABLE_SYSTEM_MALLOC, 1, [Define if System memory allocator is supported.])
fi

if test "x$format_rgba" = "xyes"; then
    AC_DEFINE(ENABLE_FORMAT_RGBA, 1, [Define if RGBA color format is supported.])
fi

if test "x$format_argb" = "xyes"; then
    AC_DEFINE(ENABLE_FORMAT_ARGB, 1, [Define if ARGB color format is supported.])
fi

if test "x$format_abgr" = "xyes"; then
    AC_DEFINE(ENABLE_FORMAT_ABGR, 1, [Define if ABGR color format is supported.])
fi

if test "x$format_bgra" = "xyes"; then
    AC_DEFINE(ENABLE_FORMAT_BGRA, 1, [Define if BGRA color format is supported.])
fi

if test "x$format_rgb" = "xyes"; then
    AC_DEFINE(ENABLE_FORMAT_RGB, 1, [Define if RGB color format is supported.])
fi

if test "x$format_bgr" = "xyes"; then
    AC_DEFINE(ENABLE_FORMAT_BGR, 1, [Define if BGR color format is supported.])
fi

if test "x$format_rgb565" = "xyes"; then
    AC_DEFINE(ENABLE_FORMAT_RGB565, 1, [Define if RGB565 color format is supported.])
fi

if test "x$format_rgb555" = "xyes"; then
    AC_DEFINE(ENABLE_FORMAT_RGB555, 1, [Define if RGB555 color format is supported.])
fi

if test "x$build_image" = "xyes"; then
    if test "x$build_gif_support" = "xyes"; then
        build_gif_support="no"
        AC_CHECK_LIB(gif, DGifOpenFileName, [
            AC_CHECK_HEADERS(gif_lib.h, [
                build_gif_support="yes"
            ])
        ])
        if test "x$build_gif_support" = "xno"; then
            AC_MSG_WARN([giflib headers and/or libs where not found, gif support disabled!])
        fi
    fi

    if test "x$build_png_support" = "xyes"; then
        build_png_support="no"
        AC_CHECK_LIB(png, png_get_copyright, [
            AC_CHECK_HEADERS(png.h, [
                build_png_support="yes"
            ])
        ])
        if test "x$build_png_support" = "xno"; then
            AC_MSG_WARN([libpng headers and/or libs where not found, png support disabled!])
        fi
    fi

    if test "x$build_jpg_support" = "xyes"; then
        build_jpg_support="no"
        AC_CHECK_LIB(jpeg, jpeg_read_header, [
            AC_CHECK_HEADERS(jpeglib.h, [
                build_jpg_support="yes"
            ])
        ])
        if test "x$build_jpg_support" = "xno"; then
            AC_MSG_WARN([libjpeg headers and/or libs where not found, jpg support disabled!])
        fi
    fi

    if test "x$build_webp_support" = "xyes"; then
        build_webp_support="no"
        AC_CHECK_LIB(webp, WebPGetDecoderVersion, [
            AC_CHECK_HEADERS(webp/decode.h, [
                build_webp_support="yes"
            ])
        ])
        if test "x$build_webp_support" = "xno"; then
            AC_MSG_WARN([libwebp headers and/or libs where not found, webp support disabled!])
        fi
    fi
fi

if test "x$build_coverage" = "xyes"; then
    CFLAGS="$CFLAGS -fprofile-arcs -ftest-coverage"
    CXXFLAGS="$CXXFLAGS -fprofile-arcs -ftest-coverage"
fi

AM_CONDITIONAL(PSX_IMAGE_SUPPORT, test "x$build_image" = "xyes")
AM_CONDITIONAL(PSX_IMAGE_GIF_SUPPORT, test "x$build_gif_support" = "xyes")
AM_CONDITIONAL(PSX_IMAGE_PNG_SUPPORT, test "x$build_png_support" = "xyes")
AM_CONDITIONAL(PSX_IMAGE_JPG_SUPPORT, test "x$build_jpg_support" = "xyes")
AM_CONDITIONAL(PSX_IMAGE_WEBP_SUPPORT, test "x$build_webp_support" = "xyes")

CFLAGS="$CFLAGS -Wall -DDLL_EXPORT=1 -DEXPORT=1 -fvisibility=hidden" 
CXXFLAGS="$CXXFLAGS -Wall -fno-rtti -fno-exceptions -DDLL_EXPORT=1 -DEXPORT=1 -fvisibility=hidden -fvisibility-inlines-hidden"

AC_OUTPUT(
Makefile
include/Makefile
src/Makefile
ext/Makefile
ext/image_loader/Makefile
ext/image_loader/png/Makefile
ext/image_loader/jpeg/Makefile
ext/image_loader/gif/Makefile
ext/image_loader/webp/Makefile
test/Makefile
demos/Makefile
)
